package com.wenjiang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectList;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wenjiang.common.lang.Result;
import com.wenjiang.common.vo.GoodTypeVo;
import com.wenjiang.entity.MallGoodType;
import com.wenjiang.mapper.MallGoodTypeMapper;
import com.wenjiang.service.MallGoodTypeService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lwj
 * @since 2021-07-20
 */
@Service
public class MallGoodTypeServiceImpl extends ServiceImpl<MallGoodTypeMapper, MallGoodType> implements MallGoodTypeService {


    public MallGoodTypeServiceImpl() {
        super();
    }


    @Override
    public Result getAllGoodType() {
        //查询一级菜单
        List<GoodTypeVo> goodTypeVos = new ArrayList<>();

        List<MallGoodType> typeList = baseMapper.selectList(new QueryWrapper<MallGoodType>().eq("type_parent_id",
                0));

        //遍历一级菜单进行赋值
        for (MallGoodType item:typeList){
            //设置子菜单
            GoodTypeVo goodTypeVo =new GoodTypeVo();
            //设置属性
            goodTypeVo.setLabel(item.getTypeName());
            goodTypeVo.setValue(item.getTypeId());
            goodTypeVo.setChildren(getChildren(item.getTypeId()));
            goodTypeVos.add(goodTypeVo);
        }



        return Result.succ(goodTypeVos);
    }

    /**
     * 进入当前等级的标签
     * @param
     * @return
     */
    @Override
    public Result getCurrentLevelType(Integer level) {

        //假设当前等级超过3级 返回失败
        if(level>3){
            return Result.fail("标签等级不能超过3级");
        }

        List<MallGoodType> typeList = baseMapper.selectList(new QueryWrapper<MallGoodType>().eq("type_level", level));

        //假设查询到为空
        if(typeList==null){
            return Result.fail("数据为空");
        }

        return Result.succ(typeList);

    }

    /**
     * 查找 当前菜单的子菜单
     * @param goodTypeParentId
     * @return
     */
    @Override
    public Result getChildrenGoodType(Integer goodTypeParentId) {

        //进行查询

        List<MallGoodType> typeList = baseMapper.selectList(new QueryWrapper<MallGoodType>().eq("type_parent_id",
                goodTypeParentId));

        if(typeList.size()==0   ||typeList==null){
            return  Result.fail("该菜单没有子菜单");
        }

        return Result.succ(typeList);
    }

    private List<GoodTypeVo> getChildren(Integer parentId){

        //查询子菜单
        List<MallGoodType> goodtypeId = baseMapper.selectList(new QueryWrapper<MallGoodType>().eq("type_parent_id",
                parentId));
        if (goodtypeId==null){
            return null;
        }

        List<GoodTypeVo> list =new ArrayList<>();
        //遍历生成
        for (MallGoodType item:goodtypeId){
            GoodTypeVo goodTypeVo =new GoodTypeVo();
            //设置属性
            goodTypeVo.setLabel(item.getTypeName());
            goodTypeVo.setValue(item.getTypeId());
            goodTypeVo.setChildren(getChildren(item.getTypeId()));
            list.add(goodTypeVo);
        }
        return list;

    }

    @Override
    public Result addGoodType(MallGoodType mallGoodType) {

        //查找父标签的结点
        MallGoodType parent = baseMapper.selectById(mallGoodType.getTypeParentId());

        //没有差出来 为1级标签
        if(mallGoodType.getTypeParentId()==0 || parent.getTypeLevel()<3){
            //进一步判断是否为空
            if(parent==null){
                mallGoodType.setTypeLevel(1);
            }else {
                //不为空则是当前标签等级+1
                mallGoodType.setTypeLevel(parent.getTypeLevel()+1);
            }
            mallGoodType.setIsDelete(0);
            save(mallGoodType);
        }

        //创建的标签等级不能超过3级 超过3级页面就不好看
        else if(parent.getTypeLevel()>=3){
            return Result.fail("创建的标签不能超过3级");
        }

        return Result.succ("添加成功");
    }


     // 进入当前的等级






    @Override
    public Result updateGoodType(MallGoodType mallGoodType) {

        //修改当前标签

        UpdateWrapper<MallGoodType> updateWrapper =new UpdateWrapper<>();

        updateWrapper.set("type_name", mallGoodType.getTypeName());
        updateWrapper.set("type_rank", mallGoodType.getTypeRank());
        updateWrapper.eq("type_id", mallGoodType.getTypeId());

        boolean update = update(updateWrapper);

        if(update){
            return Result.succ("更新成功");
        }else {
            return Result.fail("更新失败");
        }

    }

    @Override
    public Result delGoodType(Integer typeId) {

        int delete = baseMapper.deleteById(typeId);
        return Result.succ("成功删除");
    }
}
